package com.icss.ah.productForSale.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.j2ee.util.UUID;

@Service
@Transactional
public class ProductForSaleService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	public List getProductType1List(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	
	public List getProductType2List(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	
	
	public List getProductList(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	
	public Long getProductCount(String modelKey,HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne(modelKey,map);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return reValue;
	}
	
	public void productDown(String modelKey,HashMap map) {
		try {
			sqlSessionTemplate.update(modelKey,map);
			
		} catch (Exception ex) {
			ex.printStackTrace();
			
		} 
	}
	
	public void productDel(String modelKey,HashMap map) {
		try {
			sqlSessionTemplate.update(modelKey,map);
			
		} catch (Exception ex) {
			ex.printStackTrace();
			
		} 
	}
	
	public boolean addStockSave(HashMap hm){
		boolean result = false;
		try {
			Integer pcNo = sqlSessionTemplate.selectOne("ProductForSaleBean.queryBuyPc", hm);
			pcNo = pcNo == null ? 1 : pcNo;
			hm.put("uuid", new UUID().toString());
			hm.put("pcNo", pcNo);
			sqlSessionTemplate.insert("ProductForSaleBean.insertProductStock", hm);
			sqlSessionTemplate.update("ProductForSaleBean.updateProductStock", hm);
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return result;
	}
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 库存预警设置
	 * @date 2017-7-18
	 */
	public boolean alarmSettingSave(Map<String, String> map){
		boolean result = false;
		try {
			sqlSessionTemplate.update("ProductForSaleBean.alarmSettingSave", map);
			result = true;infolog.info("ProductForSaleService|alarmSettingSave|para:"+map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductForSaleService|alarmSettingSave", e);
		} 
		return result;
	}
}
